<?php

/**
 * Test MD5 hash against various online databases
 * See example at the bottom of page
 */
class Md5Decrypt
{
    
    private $md5;
    private $md5_api = array();
    
    function __construct() { 
        $this->md5_db[] = array("url" => "http://md5decoder.org/", "xpath" => '//html/body/article/h2[1]', "error" => "How it works?");
        $this->md5_db[] = array("url" => "http://md5.znaet.org/md5/", "xpath" => "//html/body/div[*]/div[*]/div[*]/h3", "error" => "md5.znaet.org");
    }
    
    /**
     * Summary of decrypt
     * @param mixed $md5 
     */
    public function decrypt($md5)
    {
        $this->md5 = $md5;
        $c = count($this->md5_db) - 1;
        
        /* Check each sources */
        for ($x=0; $x<=$c; $x++)
        {
            $result = $this->Md5GetResults($this->md5_db[$x]['url'],$this->md5_db[$x]['xpath'],$this->md5);
            
            if (isset($result))
            {
                for ($x=0; $x<=$c; $x++)
                {
                    if ($result == $this->md5_db[$x]['error'])
                    {
                        $result = "Sorry nothing found";
                        break;
                    }
                }
            }
        }
     
        print $result;
    }
    
    /**
     * Summary of Md5GetResults
     * @param mixed $api_url 
     * @param mixed $api_xpath 
     * @param mixed $md5 
     * @return mixed
     */
    public function Md5GetResults($db_url, $db_xpath, $md5)
    {
        if ($stream = fopen($db_url . $md5, 'r'))
        {
            $html = stream_get_contents($stream, -1, 10);
            fclose($stream);
        }
 
        $dom = new DOMDocument();
        $dom->loadHTML($html);
        $xpath = new DOMXPath($dom);
        $tags = $xpath->query($db_xpath);

        foreach ($tags as $tag) {
            $result =  $tag->nodeValue;
        }
        
        return $result;
    }
    
}

$md5 = new Md5Decrypt;
$md5->decrypt("7110ee3611e994c97ab38ff9e3d91423");

?>